package com.ds.yuntukubackend.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ds.yuntukubackend.annotation.AuthCheck;
import com.ds.yuntukubackend.common.BaseResponse;
import com.ds.yuntukubackend.common.DeleteRequest;
import com.ds.yuntukubackend.common.ResultUtils;
import com.ds.yuntukubackend.contant.UserConstant;
import com.ds.yuntukubackend.exception.BusinessException;
import com.ds.yuntukubackend.exception.ErrorCode;
import com.ds.yuntukubackend.exception.ThrowUtils;
import com.ds.yuntukubackend.model.dto.user.*;
import com.ds.yuntukubackend.model.entity.User;
import com.ds.yuntukubackend.model.enums.UserRoleEnum;
import com.ds.yuntukubackend.model.vo.LoginUserVO;
import com.ds.yuntukubackend.model.vo.UserVO;
import com.ds.yuntukubackend.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    /**
     * 用户注册
     * @param userRegisterRequest 用户注册请求DTO
     * @return 用户id
     */
    @PostMapping("register")
    public BaseResponse<Long> userRegister(@RequestBody UserRegisterRequest userRegisterRequest) {
        ThrowUtils.throwIf(userRegisterRequest == null, ErrorCode.PARAMS_ERROR,"参数不能为空");
        String account = userRegisterRequest.getAccount();
        String password = userRegisterRequest.getPassword();
        String checkPassword = userRegisterRequest.getCheckPassword();
        long request = userService.userRegister(account, password, checkPassword);
        return ResultUtils.success(request);
    }

    /**
     * 用户登录
     * @param userLoginRequest 用户登录请求DTO
     * @param request   请求
     * @return LoginUserVO 脱敏后的用户信息
     */
    @PostMapping("login")
    public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
        ThrowUtils.throwIf(userLoginRequest == null, ErrorCode.PARAMS_ERROR,"参数不能为空");
        String account = userLoginRequest.getAccount();
        String password = userLoginRequest.getPassword();
        LoginUserVO loginUserVO = userService.userLogin(account, password, request);
        return ResultUtils.success(loginUserVO);

    }

    /**
     * 退出登录
     * @param request
     * @return
     */
    @PostMapping("logout")
    public BaseResponse<Boolean> userLogout(HttpServletRequest request){
        ThrowUtils.throwIf(request == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.userLogout(request));
    }

    /**
     * 获取当前登录用户信息
     * @param request 请求
     * @return LoginUserVO 脱敏后的用户信息
     */
    @GetMapping("/get/loginUser")
    public BaseResponse<LoginUserVO> getLoginUser(HttpServletRequest request){
        ThrowUtils.throwIf(request == null, ErrorCode.NOT_LOGIN_ERROR);
        User user = userService.getLoginUser(request);
        LoginUserVO loginUserVO = userService.getLoginUserVO(user);
        return ResultUtils.success(loginUserVO);
    }

    /**
     * 创建用户
     * @param userCreateRequest 用户创建请求DTO
     * @return 用户id
     */
    @PostMapping("/create")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Long> creteUser(@RequestBody UserCreateRequest userCreateRequest) {
        ThrowUtils.throwIf(userCreateRequest.getAccount() == null,ErrorCode.PARAMS_ERROR,"账户不能为空");
        Long id = userService.createUser(userCreateRequest);
        return ResultUtils.success(id);
    }


    /**
     * 根据id获取用户（仅管理员可用）
     * @param id
     * @return
     */
    @GetMapping("/get")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<User> getUserById(long id){
        ThrowUtils.throwIf(id <= 0, ErrorCode.PARAMS_ERROR);
        User user = userService.getById(id);
        ThrowUtils.throwIf(user == null, ErrorCode.NOT_FOUND_ERROR);
        return ResultUtils.success(user);
    }
    /**
     * 根据id获取用户脱敏信息
     * @param id
     * @return
     */
    @GetMapping("/get/vo")
    public BaseResponse<UserVO> getUserVOById(long id){
        //TODO:getUserById,是管理员可用，可以调用吗？需测试
        BaseResponse<User> response = getUserById(id);
        User user = response.getData();
        return ResultUtils.success(userService.getUserVO(user));
    }

    /**
     * 分页获取用户列表（仅管理员可用，脱敏信息）
     * @param userQueryRequest
     * @return
     */
    @PostMapping("/get/page/vo")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Page<UserVO>> getUserByPage(@RequestBody UserQueryRequest userQueryRequest){
        ThrowUtils.throwIf(userQueryRequest == null, ErrorCode.PARAMS_ERROR);
        return ResultUtils.success(userService.getUserVOPage(userQueryRequest));
    }

    /**
     * 删除用户（仅管理员可用）
     * @param deleteRequest 删除请求DTO
     * @return
     */
    @PostMapping("/delete")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> deleteUser(@RequestBody DeleteRequest deleteRequest){
        ThrowUtils.throwIf(deleteRequest == null || deleteRequest.getId() <= 0,ErrorCode.PARAMS_ERROR);
        boolean b = userService.removeById(deleteRequest.getId());
        return ResultUtils.success(b);
    }

    /**
     * 更新用户信息（仅管理员可用）
     * @param userUpdateRequest
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = UserConstant.ADMIN_ROLE)
    public BaseResponse<Boolean> updateUser(@RequestBody UserUpdateRequest userUpdateRequest){
        ThrowUtils.throwIf(userUpdateRequest == null
                                    || userUpdateRequest.getId() == null
                                    , ErrorCode.PARAMS_ERROR);
        return  ResultUtils.success(userService.updateUser(userUpdateRequest));
    }



}
